#include "framewin.h"

#include "projectlistbox.h"
#include "blankwindow.h"
#include "texteditwin.h"

#include "../resource.h"
#include <Windows.h>

LRESULT CALLBACK FrameWndProc(HWND,UINT,WPARAM,LPARAM);

CFrameWindow::CFrameWindow() {
	LPCTSTR lpszClass = TEXT("SCTextMapEdit");
	
	//Create a window class & register
	WNDCLASS WndClass;
	memset(&WndClass, 0, sizeof(WNDCLASS));
	
	WndClass.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
	WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
	WndClass.hIcon=LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON1));
	WndClass.hInstance=g_hInstance;
	WndClass.lpfnWndProc=(WNDPROC)FrameWndProc;
	WndClass.lpszClassName=lpszClass;
	WndClass.lpszMenuName=NULL;
	WndClass.style=CS_HREDRAW | CS_VREDRAW;
	
	RegisterClass(&WndClass);


	//Create main window
	HMENU hMenu = LoadMenu(g_hInstance, MAKEINTRESOURCE(IDR_MAINMENU));
	HWND hWnd;
	hWnd=CreateWindow(lpszClass,lpszClass,WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
		NULL, hMenu, g_hInstance,NULL);
	SetHWND(hWnd);

	//create control & apply target
	m_Layout.SetApplyTarget(this);

	CListBoxWindow *list = new CListBoxWindow(this, 10000);
	m_Layout.SetWindow(m_Layout.CreateItem(), APPEND_LEFT, list, CLayout::CreatePixelPosValue(200), 0);
	m_ListBoxWindow = list;

	CBlankWindow *main = new CBlankWindow();
	m_MainWindow = m_Layout.SetWindow(m_Layout.CreateItem(), APPEND_RIGHT, main, CLayout::CreateRatioPosValue(1), 0);
	

	m_Layout.UpdatePos();
	
	ShowWindow(hWnd, SW_SHOW);
}

CFrameWindow::~CFrameWindow() { }

LRESULT CALLBACK FrameWndProc(HWND hWnd,UINT iMessage,WPARAM wParam,LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	CFrameWindow* win = static_cast<CFrameWindow*>(GetCWindow(hWnd));
	RECT rt;

	switch(iMessage) {
	case WM_PAINT:
		hdc=BeginPaint(hWnd, &ps);
		EndPaint(hWnd, &ps);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_SIZE:
		if(win) {
			GetClientRect(hWnd, &rt);
			win->m_Layout.SetRect(rt);
			win->m_Layout.UpdatePos();
		}
		return 0;

	case WM_GETMINMAXINFO:
		if(win) {
			MINMAXINFO *maxinfo = (MINMAXINFO*)lParam;
			int minw, minh;
			win->m_Layout.GetMinimumSize(&minw, &minh);
			maxinfo->ptMinTrackSize.x = minw;
			maxinfo->ptMinTrackSize.y = minh;
		}
		return 0;
	}

	return DefWindowProc(hWnd,iMessage,wParam,lParam);
}
